//Wardell Chandler Bagby
//CS 325
namespace Project3
{
	using namespace System;
	using namespace System::IO;
	using namespace System::Collections::Generic;	
	public ref class Email
	{
	public:
		String^ getMessage(){return "Test Message Available.";};
	};
	ref class SpamFilter
	{
	private:
		List<Entry^>^ spamProb;
		FileStream^ spamFile;
		int numberInSpam;
		double median;
		int numberInHam;
		Email^ email;
		bool isSpam;
		void setSpamFile(FileStream^ f)
		{
			spamFile = f;
		};
		void setEmail(Email^ e)
		{
			email = e;
		};
	public:
		SpamFilter(FileStream^ f)
		{
			setSpamFile(f);
			spamProb = gcnew List<Entry^>();
			median = .5;
		};
		void readInSpamFile()
		{
			StreamReader^ sR = gcnew StreamReader(spamFile);	
			String^ line;
			String^ delim = ":\n";
			while((line = sR->ReadLine())!=nullptr)
			{
				array<String^>^ words = line->Split(delim->ToCharArray());
				if(words->Length==5)
				spamProb->Add(gcnew Entry(words[0],Convert::ToInt32(words[1]),Convert::ToInt32(words[2]),Convert::ToInt32(words[3]),Convert::ToInt32(words[4])));
				Console::WriteLine(spamProb[spamProb->Count-1]->ToString());
			}
		};
		void setMedian(double med)
		{
			median = med;
		};
		FileStream^ getSpamFile()
		{
			return spamFile;
		};
		double getHamPercentage(String^ word)
		{ 
			double result = spamProb->BinarySearch(gcnew Entry(word));
			if(result>=0)
				return spamProb[result]->getHamPercentage();
			return median;
		};
		double getSpamPercentage(String^ word)
		{ 
			double result = spamProb->BinarySearch(gcnew Entry(word));
			if(result>=0)
				return spamProb[result]->getSpamPercentage();
			return median;
		};
		double calculateProbablity(Email^ e)
		{
			String^ message = e->getMessage();
			List<double>^ spamPercentages = gcnew List<double>;
			List<double>^ hamPercentages = gcnew List<double>;
			array<double>^ topValueIndexes = gcnew array<double>(15);
			String^ delims = " \n\r:?.!@*,";
			array<String^>^ words = message->Split(delims->ToCharArray());
			for(int i=0; i<words->Length; i++)
			{
					spamPercentages->Add(getSpamPercentage(words[i]));
					hamPercentages->Add(getHamPercentage(words[i]));
			}
			for(int i=0; i<15; i++)
			{
				topValueIndexes[i] = hamPercentages[i];
			}
			for(int i=0; i<words->Length; i++)
			{
				int minIndex = 0;
				for(int j=1; j<topValueIndexes->Length; j++)
				{
					if(topValueIndexes[minIndex]>topValueIndexes[j])
					{
						minIndex = j;
					}
				}
				if(spamPercentages[i]>topValueIndexes[minIndex])
					topValueIndexes[minIndex] = spamPercentages[i];
				if(hamPercentages[i]>topValueIndexes[minIndex])
					topValueIndexes[minIndex] = hamPercentages[i];
			}
			return 0;//spamFilterIndicatorValueEquation(topValueIndexes,topValueIndexes->Length);
		};

	};
}